import json

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.
def load_test(request):

    return render(request, 'load_test.html')

def load_test_server(request):

    return render(request, 'load_test_server.html')

def jquery_get(request):

    return render(request, 'jquery_get.html')

def jquery_get_server(request):
    #获取get请求参数
    uname = request.GET.get('uname', 'meinadao')
    age = request.GET.get('age', 'age meinadao')

    d = {'uname': uname, 'age': age}

    #指定httpresponse响应头
    return HttpResponse(json.dumps(d), content_type='application/json')


def jquery_post(request):

    return render(request, 'jquery_post.html')

def jquery_post_server(request):

    if int(request.POST.get('age', 0)) > 100:
        d = {'msg': 'Your age is so big!', 'code': 1201}
        return HttpResponse(json.dumps(d), content_type='application/json')

    d = {'msg':'post is ok', 'code': 1202}
    return HttpResponse(json.dumps(d), content_type='application/json')

def jquery_ajax(request):
    return render(request, 'jquery_ajax.html')

def jquery_ajax_server(request):
    import time
    time.sleep(3)
    d = {'name': 'guoxiaonao', 'age': 18}

    return HttpResponse(json.dumps(d), content_type='application/json')

def jquery_ajax_user(request):
    return render(request, 'jquery_ajax_user.html')

def jquery_ajax_user_server(request):

    d = [{'name':'guoxiaonao', 'age':18}, {'name':'laowang', 'age':38}]

    return HttpResponse(json.dumps(d), content_type='application/json')


def cross(request):

    return render(request, 'cross.html')

def cross_server(request):
    func = request.GET.get('callback')
    #func('str')
    return HttpResponse(func + "('wo kua chu lai le hahahaha')")

def cross_server_json(request):

    func = request.GET.get('callback')
    d = {'name':'guoxiaonao', 'age':18}

    return HttpResponse(func + "(" + json.dumps(d) + ")")































